package implementation.sort;

import anaydis.sort.SorterType;
import com.sun.istack.internal.NotNull;

import java.util.Comparator;
import java.util.List;


public class HSort extends AbstractSorter {
    private int h;

    public HSort() {
        this(SorterType.H);
    }

    protected HSort(SorterType type) {
        super(type);
    }


    public <T> void hSort(@NotNull Comparator<T> tComparator, @NotNull List<T> ts) {
        if (h == 0) {
            sort(tComparator, ts, 11);
        } else {
            sort(tComparator, ts, h);
        }
    }

 /*   protected <T> void sort(@NotNull Comparator<T> tComparator, @NotNull List<T> ts, int h) {
        int k = 0;
        while (k < h) {
            int i = 0;
            boolean b = true;

            while (i < (ts.size() / h) - 1 && b) {
                b = false;
                for (int j = h + k; j < ts.size() - i; j += h) {
                    if (greater(tComparator, ts, j - h, j)) {
                        swap(ts, j - h, j);
                        b = true;
                    }
                }
                i++;
            }
            k++;
        }

    }*/

     protected <T> void sort(@NotNull Comparator<T> tComparator, @NotNull List<T> ts, int h) {
         int n = ts.size();
         boolean change = true;
         for(int i=0; i<h; i++){
             for(int k = n-h; k>0; k-=h){
                 if(!change){
                     break;
                 }
                 change = false;
                 for(int j=i; j<k; j+=h){
                     if(greater(tComparator, ts, j, j+h)){
                         swap(ts, j, j+h);
                         change = true;
                     }
                 }
             }

         }
     }


    public void setH(int h) {
        this.h = h;
    }

    protected <T> void endSort(@NotNull Comparator<T> tComparator, @NotNull List<T> ts) {

    }


    public <T> void sort(@NotNull Comparator<T> tComparator, @NotNull List<T> ts) {
        hSort(tComparator, ts);
    }
}
